package com.classroom.smart.controller;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.classroom.smart.constant.MessageConstant;
import com.classroom.smart.dto.PageDTO;
import com.classroom.smart.pojo.Video;
import com.classroom.smart.result.PageResult;
import com.classroom.smart.result.Result;
import com.classroom.smart.service.VideoService;
import com.classroom.smart.utils.VideoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/publishVideo")
public class VideoController {

    @Autowired
    private VideoService videoService;

    //检查所有视频信息
    @GetMapping("findAll")
    public Result findAll(){
        List<Video> list = videoService.findAll();
        if (list!=null && list.size()>0){
            return new Result(true, MessageConstant.QUERY_VIDEO_SUCCESS,list);
        }
        return new Result(false,MessageConstant.QUERY_VIDEO_FAIL);
    }

    @PostMapping("upload")
    public Result upload(@RequestPart("videoFile")MultipartFile videoFile){
        HashMap hashMap = new HashMap();
        try{
            if (videoFile!=null){
                String accessKeyId = "LTAI5tQqjmxN4FdQeweiSvMn";
                String accessKeySecret = "21xncpuDQxuHtSwnCzgNrmCaT0m5Ii";
                String title = videoFile.getOriginalFilename();
                String fileName = videoFile.getOriginalFilename();
                InputStream inputStream = videoFile.getInputStream();

                //1.上传视频的方法
                UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
                UploadVideoImpl uploader = new UploadVideoImpl();
                UploadStreamResponse response = uploader.uploadStream(request);
                System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
                if (response.isSuccess()) {
                    System.out.print("VideoId=" + response.getVideoId() + "\n");
                } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                    System.out.print("VideoId=" + response.getVideoId() + "\n");
                    System.out.print("ErrorCode=" + response.getCode() + "\n");
                    System.out.print("ErrorMessage=" + response.getMessage() + "\n");
                }
                String id = response.getVideoId();
                hashMap.put("id", id);
                hashMap.put("fileName", fileName);
                return new Result(true, MessageConstant.VIDEO_UPLOAD_SUCCESS,hashMap);
            }else {
                throw new RuntimeException("文件为空");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.VIDEO_UPLOAD_FAIL);
    }

    //分页查询
    @GetMapping("findPage")
    public PageResult findPage(PageDTO pageDTO){
        PageResult page = videoService.findPage(pageDTO);
        return page;
    }

    //新增视频
    @PostMapping("add")
    public Result add(@RequestBody Video video){
        try{
            videoService.add(video);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_VIDEO_FAIL);
        }
        return new Result(true,MessageConstant.ADD_VIDEO_SUCCESS);
    }

    //通过视频ID查询信息
    @GetMapping("{videoId}")
    public Result viewOne(@PathVariable("videoId") Integer videoId){
        Video video = videoService.viewOne(videoId);
        if (video!=null){
            return new Result(true,MessageConstant.QUERY_VIDEO_SUCCESS,video);
        }
        return new Result(false,MessageConstant.QUERY_VIDEO_FAIL);
    }

    //修改信息
    @PutMapping("update")
    public Result update(@RequestBody Video video){
        try{
            videoService.update(video);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_VIDEO_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_VIDEO_SUCCESS);
    }

    //根据视频ID删除
    @DeleteMapping("{videoId}")
    public Result delete(@PathVariable Integer videoId){
        try{
            videoService.delete(videoId);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_VIDEO_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_VIDEO_SUCCESS);
    }
}
